import sys
from PyQt5.QtWidgets import QApplication, QDialog
from tool_dialogs.Ui_tool_dialogs import Ui_Dialog  

class ToolsDialog(QDialog, Ui_Dialog):  # 继承生成的Dialog类
    def __init__(self, parent=None):
        super(ToolsDialog, self).__init__(parent)
        self.setupUi(self)  # 设置UI
        self.init_ui()  # 初始化其他组件
        
    def init_ui(self):
       self.pushButton_ok.clicked.connect(self.ok_event)  
       self.pushButton_reset.clicked.connect(self.reset_event)
       # lineEdit_weight文本改变后触发的槽函数
       self.lineEdit_weight.textChanged.connect(self.change_event)
       # lineEdit_vol_per_weight文本改变后触发的槽函数
       self.lineEdit_vol_per_weight.textChanged.connect(self.change_event)
       # 切换勾选框checkBox_vol_exp的状态后触发的槽函数
       self.checkBox_vol_exp.stateChanged.connect(self.change_event)
       # 选中单选按钮radioButton_LD触发的槽函数
       self.radioButton_LD.toggled.connect(self.radioButton_change_event)
       # 选中单选按钮radioButton_GS触发的槽函数
       self.radioButton_MD.toggled.connect(self.radioButton_change_event)
       # 选中单选按钮radioButton_NS触发的槽函数
       self.radioButton_SD.toggled.connect(self.radioButton_change_event)
       
    def radioButton_change_event(self):
        if self.radioButton_LD.isChecked():
            self.lineEdit_vol_per_weight.setText("120")
        elif self.radioButton_MD.isChecked():
            self.lineEdit_vol_per_weight.setText("150")
        elif self.radioButton_SD.isChecked():
            self.lineEdit_vol_per_weight.setText("180")
            # 选中复选框checkBox_vol_exp
            self.checkBox_vol_exp.setChecked(True)
            return
            
        self.change_event()
       
    def ok_event(self):
        # 清除label_error的内容
        self.label_error.clear()
        # 获取两个输入框的文本内容并转换为整数类型
        # 体重
        weight = self.lineEdit_weight.text()
         # 补液目标
        vol_per_weight = self.lineEdit_vol_per_weight.text()
     
        
        # 判断是不是为数字形式的文本
        if not weight.isdigit():
            self.label_error.setText("体重输入错误！")
            return
        if not vol_per_weight.isdigit():
            self.label_error.setText("补液目标输入错误！")
            return
        weight = int(weight)
        vol_per_weight = int(vol_per_weight)
       
        # 补液总量 = 体重 * 补液目标
        total_vol = weight * vol_per_weight
        self.textBrowser_result.setText("补液总量: " + str(total_vol) + "mL")
        self.textBrowser_result.append("-"*20)
        
        # 获取checkBox_vol_exp的状态，如果是true，则需要计算扩容量
        vol_exp_flag = self.checkBox_vol_exp.isChecked()
        if vol_exp_flag:
            vol_exp = weight * 20 
            if vol_exp > 300:
                vol_exp = 300
            
            self.textBrowser_result.append("扩容量: "+ str(vol_exp) + "mL(2：1等张液按20ml/kg计算，30分钟内滴完，总量小于300mL)")
            self.textBrowser_result.append(f"方便计算：5%GS {vol_exp:>6.1f}mL, 10%NS {vol_exp/100*6:>6.1f}mL, 5%NaHCO3 {vol_exp/100*10:>6.1f}mL")
            self.textBrowser_result.append(f"精确计算：5%GS {vol_exp-vol_exp/3*2/10-vol_exp/3/5*1.4:>6.1f}mL, 10%NS {vol_exp/3*2/10:>6.1f}mL, 5%NaHCO3 {vol_exp/3/5*1.4:>6.1f}mL")
            self.textBrowser_result.append("-"*20)
            
        
        # 计算累计丢失量
        if vol_exp_flag:
            lost_vol = total_vol/2 - vol_exp
        else:
            lost_vol = total_vol/2
        self.textBrowser_result.append("累计丢失量: " + str(lost_vol) + "mL(输注8-12小时内，或8-10ml/kg/h)")
        # 计算所需3:2:1液体的量
        self.textBrowser_result.append("·需3:2:1液的量（1/2张力，用于等渗性脱水）: ")
        self.textBrowser_result.append(f"方便计算：5%GS {lost_vol:>6.1f}mL, 10%NS {lost_vol/100*3:>6.1f}mL, 5%NaHCO3 {lost_vol/100*5:>6.1f}mL")
        self.textBrowser_result.append(f"精确计算：5%GS {lost_vol-lost_vol/6*2/10-lost_vol/6/5*1.4:>6.1f}mL, 10%NS {lost_vol/6*2/10:>6.1f}mL, 5%NaHCO3 {lost_vol/6/5*1.4:>6.1f}mL")
        self.textBrowser_result.append(" "*20)
        # 计算所需4:3:2液体的量
        self.textBrowser_result.append("·需4:3:2液的量（2/3张力，用于低渗性脱水）: ")
        self.textBrowser_result.append(f"方便计算：5%GS {lost_vol:>6.1f}mL, 10%NS {lost_vol/100*4:>6.1f}mL, 5%NaHCO3 {lost_vol/100*7:>6.1f}mL")
        self.textBrowser_result.append(f"精确计算：5%GS {lost_vol-lost_vol/9*4/10-lost_vol/9*2/5*1.4:>6.1f}mL, 10%NS {lost_vol/9*4/10:>6.1f}mL, 5%NaHCO3 {lost_vol/9*2/5*1.4:>6.1f}mL")
        

        self.textBrowser_result.append("-"*20)
        
        
    def change_event(self):
        # 体重
        weight = self.lineEdit_weight.text()
         # 补液目标
        vol_per_weight = self.lineEdit_vol_per_weight.text()
        # 判断是否为空
        if not weight or not vol_per_weight:
            return
        
        self.ok_event()  # 触发ok_event槽函数
    def reset_event(self):
        # 处理重置按钮的点击事件:清空两个输入框lineEdit_weight和lineEdit_vol_per_weight的内容
        self.lineEdit_weight.clear()
        self.lineEdit_vol_per_weight.clear()
        
        
    def show_dialog(self):
        self.exec_()  # 显示弹出窗口

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ToolsDialog()  # 创建新的Dialog窗口
    window.show()
    sys.exit(app.exec_())
